package com.virtium.vpc.action.controller;

import java.util.ArrayList;
import java.util.Iterator;

import com.virtium.vpc.dao.DAOFactory;
import com.virtium.vpc.dao.VpcPackageDAO;
import com.virtium.vpc.hibernate.model.VpcPackage;

public class VpcPackageManager {
	private static ArrayList<VpcPackage> arrVpcPackage = null;

	public static ArrayList<VpcPackage> getAll() {
		try {
			if (arrVpcPackage != null && arrVpcPackage.size() > 0)
				return arrVpcPackage;
			VpcPackageDAO VpcPackageDAO = DAOFactory.getInstance()
					.getVpcPackageDAO();
			arrVpcPackage = new ArrayList<VpcPackage>();
			arrVpcPackage.addAll(VpcPackageDAO.findAll());
		} catch (Exception ex) {
		}
		return arrVpcPackage;
	}
	
	public static String getTextByID(int id){
		getAll();
		Iterator<VpcPackage> iterObject = arrVpcPackage.listIterator();
		while(iterObject.hasNext()){
			VpcPackage object = iterObject.next();
			if(object.getId().equals(id))return object.getText();
			
		}
		return "";
	}
	
	public static void AddNew(String text){
		try{
			VpcPackage object = new VpcPackage();
			object.setText(text);
			VpcPackageDAO ObjectDAO = DAOFactory.getInstance().getVpcPackageDAO();
			ObjectDAO.save(object);
			arrVpcPackage = null;
		}catch(Exception ex){
			
		}
	}
	
	public static void Delete(int id){
		try{
			
			VpcPackageDAO ObjectDAO = DAOFactory.getInstance().getVpcPackageDAO();
			VpcPackage object = ObjectDAO.findById(id);
			ObjectDAO.delete(object);
			arrVpcPackage = null;
		}catch(Exception ex){
			
		}
	}
}
